package com.hsmw.api.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hsmw.api.mapper.HsmwMenuMapper;
import com.hsmw.api.mapper.HsmwRoleMapper;
import com.hsmw.api.vo.MenuListVO;
import com.hsmw.common.base.BaseService;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.domain.HsmwMenu;

import com.hsmw.domain.HsmwRole;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.htn.common.core.constant.CommonConstants.ResponseStatus.FAIL;
import static com.htn.common.core.constant.CommonConstants.ResponseStatus.SUCCESS;

/**
 * @author hongfei
 */
@Service
public class HsmwMenuService extends BaseService<HsmwMenuMapper, HsmwMenu> {

	@Autowired
	private HsmwMenuMapper hsmwMenuMapper;
	@Autowired
	private HsmwRoleMapper hsmwRoleMapper;

	/**
	 * list分页
	 *
	 * @param page
	 * @param hsmwMenu
	 * @return
	 */
	public IPage<HsmwMenu> getHsmwMenuPage(IPage page, HsmwMenu hsmwMenu) {
		LambdaQueryWrapper<HsmwMenu> queryWrapper = new LambdaQueryWrapper<>();
		IPage ipage = this.baseMapper.selectPage(page, queryWrapper.orderByDesc(HsmwMenu::getCreateTime));
		return ipage;
	}

	/**
	 * 查询菜单树
	 *
	 * @param roleId
	 * @return
	 */
	public HashMap<Object, Object> getHsmwMenuList(String roleId) {
		HashMap<Object, Object> map = Maps.newHashMap();
		List<String> collect = Lists.newArrayList();
		LambdaQueryWrapper<HsmwMenu> queryWrapper = new LambdaQueryWrapper<>();
		List<HsmwMenu> data = hsmwMenuMapper.selectList(queryWrapper);
		HsmwRole hsmwRole = hsmwRoleMapper.selectById(roleId);
		if (StrUtil.isNotBlank(hsmwRole.getMenuTree())) {
			JSONArray jsonArray = new JSONArray(hsmwRole.getMenuTree());
			ArrayList<HsmwMenu> hsmwMenus = jsonArray.toList(HsmwMenu.class);
//			Iterator<HsmwMenu> iterator = hsmwMenus.iterator();
//			while (iterator.hasNext()) {
//				HsmwMenu hsmwMenu = iterator.next();
//				JSONObject jsonObject = new JSONObject(hsmwMenu.getMeta());
//				if (jsonObject.getBool("show") != null && !jsonObject.getBool("show")) {
//					iterator.remove();
//				} else {
//					roleMenuList.add(hsmwMenu);
//				}
//			}
			collect = hsmwMenus.stream().map(HsmwMenu::getId).collect(Collectors.toList());
		}
		map.put("allMenu", data);
		map.put("roleMenu", collect);
		return map;
	}

	/**
	 * 根据ID查询
	 *
	 * @param id
	 * @return
	 */
	public HsmwMenu getById(String id) {
		LambdaQueryWrapper<HsmwMenu> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(HsmwMenu::isFlag, true);
		queryWrapper.eq(HsmwMenu::getId, id);
		return hsmwMenuMapper.selectOne(queryWrapper);
	}

	/**
	 * 新增或修改 逻辑删除
	 *
	 * @return
	 */
	public DataResponse saveOrUpdateHsmwMenu(HsmwMenu hsmwMenu) {
		StringBuffer msg = new StringBuffer();
		Integer result = 0;
		if (StringUtils.isNotBlank(hsmwMenu.getId())) {
			result = hsmwMenuMapper.updateById(hsmwMenu);
			msg.append("修改成功");
		} else {
			hsmwMenu.setFlag(false);
			result = hsmwMenuMapper.insert(hsmwMenu);
			msg.append("添加成功");
		}
		return new DataResponse(SUCCESS);
	}
}
